import { NextRequest, NextResponse } from 'next/server';
import { UserModel } from '@/models/user';

export async function POST(request: NextRequest) {
  try {
    // 生成随机用户名以避免冲突
    const randomSuffix = Math.floor(Math.random() * 10000);
    const testUser = {
      username: `testuser${randomSuffix}`,
      password: 'testpassword123',
      email: `testuser${randomSuffix}@example.com`
    };
    
    console.log('尝试创建测试用户:', testUser);
    
    // 创建用户
    const userId = await UserModel.create(testUser);
    
    return NextResponse.json({
      status: 'success',
      message: '测试用户创建成功',
      userId,
      username: testUser.username,
      timestamp: new Date().toISOString()
    });
  } catch (error) {
    console.error('测试用户创建失败:', error);
    
    return NextResponse.json({
      status: 'error',
      message: '测试用户创建失败',
      error: error instanceof Error ? error.message : '未知错误',
      timestamp: new Date().toISOString()
    }, { status: 500 });
  }
}
